package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.Account;
import com.pansoft.openplanet.bean.CreateChildWalletBean;
import com.pansoft.openplanet.constant.IPAssetsConstant;
import com.pansoft.openplanet.db.AccountDao;
import com.pansoft.openplanet.util.TCAccountManager;
import com.utilcode.util.ToastUtils;

/**
 * 选择钱包类型
 * Created by maming on 19-3-26.
 */
public class ChooseWalletTypeActivity extends BaseActivity implements View.OnClickListener {
    //
    private TextView tv_title;
    private CheckBox checkBox_wallet_cold_choose, checkBox_wallet_hot_choose;
    private Button button_next_step;
    //钱包bean
    private CreateChildWalletBean childWalletBean;

    /**
     * 是否选中冷钱包的标记
     */
    private boolean isChooseWalletCold = false;


    public static void start(Context context, CreateChildWalletBean childWalletBean) {
        Intent starter = new Intent(context, ChooseWalletTypeActivity.class);
        starter.putExtra("childWalletBean", childWalletBean);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_choose_wallet_type);
        initView();
        initData();
        initListener();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        checkBox_wallet_cold_choose = (CheckBox) findViewById(R.id.checkBox_wallet_cold_choose);
        checkBox_wallet_cold_choose.setChecked(false);
        checkBox_wallet_hot_choose = (CheckBox) findViewById(R.id.checkBox_wallet_hot_choose);
        checkBox_wallet_hot_choose.setChecked(true);
        findViewById(R.id.cv_cold).setOnClickListener(this);
        findViewById(R.id.cv_hot).setOnClickListener(this);
        findViewById(R.id.imageView_wallet_cold_mark).setOnClickListener(this);
        findViewById(R.id.imageView_wallet_hot_mark).setOnClickListener(this);
        button_next_step = (Button) findViewById(R.id.button_next_step);
        button_next_step.setOnClickListener(this);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        //设置界面的标题
        tv_title.setText("选择钱包类型");
        if (getIntent().getExtras() != null) {
            childWalletBean = (CreateChildWalletBean) getIntent().getExtras().get("childWalletBean");
        }
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        //冷钱包按钮的点击事件
        checkBox_wallet_cold_choose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    isChooseWalletCold = isChecked;
                    checkBox_wallet_hot_choose.setChecked(!isChecked);
            }
        });
        //热钱包按钮的点击事件
        checkBox_wallet_hot_choose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                isChooseWalletCold = !isChecked;
                checkBox_wallet_cold_choose.setChecked(!isChecked);
            }
        });
    }

    /**
     * 将子钱包添加到服务器
     */
    private void addChildWalletToServer() {
        if (childWalletBean == null) {
            return;
        }
        final String address = childWalletBean.getWalletAddress();
        int chainId = childWalletBean.getChainId();
        showLoading(R.string.common_text_please_wait);
        final AccountDao accountDao = new AccountDao(this);
        Account account = accountDao.getAccount(address);
        //区块名称（默认eth）
        String blockChainName = account.getBlockChainName();
        //btc的加密助记词
        String secretWord = "";
        if (chainId == IPAssetsConstant.CHAIN_ID_BTC || chainId == IPAssetsConstant.CHAIN_ID_BCH) {
            //btc
//            blockChainName = IPAssetsConstant.ChainEnum.BTC.chainName;
            secretWord = account.getSecretMnemonic();
        }
        //调用接口创建新钱包
        new TCAccountManager(this).addChildHdBtcWallet(childWalletBean.getPublicKey(),
                childWalletBean.getPrivateKey(), address, childWalletBean.getWalletAvatar(),
                childWalletBean.getWalletName(), blockChainName
                , secretWord, childWalletBean.getHdBtcXpub(), new TCAccountManager.AccountManagerListener() {
                    @Override
                    public void onSuccess(Object object) {
                        dismissLoading();
                        ToastUtils.showShort(R.string.open_planet_create_success);
                        changeAccountInfo(accountDao, address);
                        finishAffinity();
                    }

                    @Override
                    public void onFail(String fail) {
                        dismissLoading();
                        ToastUtils.showShort(R.string.open_planet_create_fail_please_retry);
                    }
                });
    }

    /**
     * 保存钱包信息
     */
    private void changeAccountInfo(AccountDao accountDao, String address) {
        Account account = accountDao.getAccount(address);
        if (account != null) {
            account.setEnable(true);
            accountDao.insertOrReplace(account);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button_next_step) {
            if (isChooseWalletCold) {
                //创建冷钱包进入新界面
                ChooseEquipmentTypeActivity.start(this, childWalletBean);
            } else {
                addChildWalletToServer();
            }
        } else if (id == R.id.cv_cold) {
            checkBox_wallet_cold_choose.setChecked(true);
        } else if (id == R.id.cv_hot) {
            checkBox_wallet_hot_choose.setChecked(true);
        } else if (id == R.id.imageView_wallet_hot_mark) {
            WalletTypeIntroActivity.start(this, WalletTypeIntroActivity.WALLET_TYPE_HOT);
        } else if (id == R.id.imageView_wallet_cold_mark) {
            WalletTypeIntroActivity.start(this, WalletTypeIntroActivity.WALLET_TYPE_COLD);
        }
    }
}
